package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.ClientCompanyMapper;
import com.erp.programmer.domain.ClientCompany;
import com.erp.programmer.dto.ClientCompanyDto;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.service.IClientCompanyService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Takagi3
 * @since 2021-05-23
 */
@Service
public class ClientCompanyServiceImpl extends ServiceImpl<ClientCompanyMapper, ClientCompany> implements IClientCompanyService {

    @Autowired
    ClientCompanyMapper clientCompanyMapper;

    @Autowired
    IClientCompanyService iClientCompanyService;

    @Override
    public  ResponseDto<List<ClientCompanyDto>> getData( ) {
        QueryWrapper<ClientCompany> queryWrapper = new QueryWrapper<>();
        List<ClientCompany> clientCompaniesListDb = clientCompanyMapper.selectList(queryWrapper);
        List<ClientCompanyDto> clientCompanyDtoList = CopyUtil.copyList(clientCompaniesListDb, ClientCompanyDto.class);
        return ResponseDto.success(clientCompanyDtoList);
    }
    /**
     * 分页
     * 搜索获取全部客户公司信息
     */
    public ResponseDto<PageDto<ClientCompanyDto>> getAll( PageDto<ClientCompanyDto> pageDto) {
        QueryWrapper<ClientCompany> queryWrapper = new QueryWrapper<>();
        if("name".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据角色名称搜索
            queryWrapper.like("name",pageDto.getSearchContent());
        }
        if(pageDto.getPage() == null){
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<ClientCompany> list = iClientCompanyService.list(queryWrapper);
        List<ClientCompanyDto> clientCompanyDtos = CopyUtil.copyList(list, ClientCompanyDto.class);
        PageInfo<ClientCompany> pageInfo = new PageInfo<>(list);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(clientCompanyDtos);
        return ResponseDto.success(pageDto);
    }

    /**
     *保存、修改客户公司信息
     */
    public ResponseDto<PageDto<ClientCompanyDto>> saveClientCompany( ClientCompanyDto clientCompanyDto) {


        ClientCompany clientCompany = CopyUtil.copy(clientCompanyDto, ClientCompany.class);
        if(CommonUtil.isEmpty(clientCompany.getId())){
            //插入
            clientCompany.setId(null);
            if (!iClientCompanyService.save(clientCompany)) {
                return ResponseDto.errorByMsg(CodeMsg.USER_ADD_ERROR);
            }
            PageDto<ClientCompanyDto> pageDtoList = getAll(new PageDto<ClientCompanyDto>()).getData();
            ResponseDto<PageDto<ClientCompanyDto>> responseDto = getAll(pageDtoList);
            responseDto.setMsg("添加成功！");
            return responseDto;
        }else{
            //修改
            if (!iClientCompanyService.updateById(clientCompany)) {
                return ResponseDto.errorByMsg(CodeMsg.USER_EDIT_ERROR);
            }
            PageDto<ClientCompanyDto> pageDtoList = getAll(new PageDto<ClientCompanyDto>()).getData();
            ResponseDto<PageDto<ClientCompanyDto>> responseDto = getAll(pageDtoList);
            responseDto.setMsg("修改成功！");
            return responseDto;
        }
    }

    /**
     * 分页
     *删除客户公司信息
     */

    public ResponseDto<PageDto<ClientCompanyDto>> deleteClientCompany( ClientCompanyDto clientCompanyDto) {

        if(!iClientCompanyService.removeById(clientCompanyDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.USER_DELETE_ERROR);
        }
        PageDto<ClientCompanyDto> pageDtoList = getAll(new PageDto<ClientCompanyDto>()).getData();
        ResponseDto<PageDto<ClientCompanyDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("删除成功！");
        return responseDto;
    }


    public ResponseDto<List<ClientCompanyDto>> add(ClientCompanyDto clientCompanyDto)
    {
        // 1.转换
        ClientCompany clientCompany = CopyUtil.copy(clientCompanyDto, ClientCompany.class);
        // 2.业务逻辑
        clientCompanyMapper.insert(clientCompany);
        //return ResponseDto.errorByMsg(CodeMsg.USER_PASSWD_EDIT_ERROR);


        return getData();
    }

    @Override
    public ResponseDto<List<ClientCompanyDto>> delete(ClientCompanyDto clientCompanyDto) {
        // 1.转换
        ClientCompany clientCompany = CopyUtil.copy(clientCompanyDto, ClientCompany.class);
        // 2.业务逻辑
        clientCompanyMapper.deleteById(clientCompany.getId());
        //return ResponseDto.errorByMsg(CodeMsg.USER_PASSWD_EDIT_ERROR);



        return getData();
    }

    @Override
    public ResponseDto<List<ClientCompanyDto>> edit(ClientCompanyDto clientCompanyDto) {
        // 1.转换
        ClientCompany clientCompany = CopyUtil.copy(clientCompanyDto, ClientCompany.class);
        // 2.业务逻辑
        clientCompanyMapper.updateById(clientCompany);
        //return ResponseDto.errorByMsg(CodeMsg.USER_PASSWD_EDIT_ERROR);


        return getData();
    }

    @Override
    public ResponseDto<List<ClientCompanyDto>> search(ClientCompanyDto clientCompanyDto) {

        // 1.转换
        ClientCompany clientCompany = CopyUtil.copy(clientCompanyDto, ClientCompany.class);
        // 2.业务逻辑
        List<ClientCompanyDto> clientCompanyDtoList = new ArrayList<>();
        QueryWrapper<ClientCompany> queryWrapper = new QueryWrapper<>();
        if(!CommonUtil.isEmpty(clientCompanyDto.getSearchType()) && !CommonUtil.isEmpty(clientCompanyDto.getSearchContent())){
            // 用户点击了搜索
            if("name".equals(clientCompanyDto.getSearchType())){
                queryWrapper.like("name", clientCompanyDto.getSearchContent());
            }
        }
        List<ClientCompany> clientCompaniesListDb = clientCompanyMapper.selectList(queryWrapper);
        clientCompanyDtoList = CopyUtil.copyList(clientCompaniesListDb, ClientCompanyDto.class);

        return ResponseDto.success(clientCompanyDtoList);
    }
}
